package org.example.demo.controller;

import org.example.demo.model.MessageInfo;
import org.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RequestMapping("/message")
@RestController
public class MessageController {


    @Autowired
    private MessageService messageService;
    //提交留言
    @RequestMapping("/publish")
    public Boolean publishMessage(MessageInfo messageInfo) {
        //进行参数的校验
        if (!StringUtils.hasLength(messageInfo.getFrom()) ||
                !StringUtils.hasLength(messageInfo.getTo()) ||
                !StringUtils.hasLength(messageInfo.getMessage()) )  {
            return false;
        }

        messageService.publishMessage(messageInfo);
        return true;  //添加成功
    }

    //查看留言
    @RequestMapping("/getList")
    public List<MessageInfo> getMessages() {
        return messageService.getMessageList();
    }
}
